package run.siyuan.rocketmq.producer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import run.siyuan.common.model.rocketmq.Message;

/**
 * {@code ClassName:} ProducerService
 * {@code Description:} TODO
 * {@code Author:} siyuan
 * {@code Date:} 2023/4/4 23:22
 */
@Service
@Slf4j
public class ProducerService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 测试发送消息
     *
     * @param message
     * @return
     */
    public void testSend(Message message) {
        String destination = message.getTopic() + ":" + message.getTag();
        SendResult sendResult = rocketMQTemplate.syncSend(destination,message.getBody());
        log.info("发送消息返回结果:{}",sendResult);

    }

    /**
     * 测试 sqlFilter
     *
     * @param message
     */
    public void testSendSqlFilter(Message message) {
        String destination = message.getTopic() + ":" + message.getTag();
        JSONObject json = JSONUtil.parseObj(message.getBody());
        org.springframework.messaging.Message<String> rocMessage = MessageBuilder.withPayload(message.getBody())
                .setHeader("name", json.getStr("name"))
                .build();
        SendResult sendResult = rocketMQTemplate.syncSend(destination, rocMessage);
        log.info("发送消息返回结果:{}",sendResult);
    }
}
